package cn.zcx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

/**
 * description:排序
 * author:zcx
 * lenovo
 * 时间：2022.02.11.15.16
 */
public class Px {
    public static void  main(String[] args){
        Integer[] array ={88,34,99,12,56,39,34,65,12,65};
        System.out.println("排序前");
        System.out.println(Arrays.toString(array));
        //升序
        Arrays.sort(array);
        System.out.println("升序"+Arrays.toString(array));
        //降序
        Arrays.sort(array,(a,b)->b-a);
        System.out.println("降序"+Arrays.toString(array));
        //乱序
        Random r=new Random();
        Arrays.sort(array, (a, b) -> r.nextBoolean() ? -1 : 1);
        System.out.println("乱序"+Arrays.toString(array));
        //Array 转换为 List 使用 shuffle打乱顺序，并转换为Array
        var list = new ArrayList<>(Arrays.stream(array).toList());
        Collections.shuffle(list);
        list.toArray(array);
        System.out.println("乱序"+Arrays.toString(array));
        System.out.println("冒泡排序前");
        System.out.println(Arrays.toString(array));
      //  外循环控制要交换多少轮
        for(int i=0;i<array.length-1;i++){
        //内循环控制每轮交换次数
            for(int j=0;j<array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    array[j]=array[j]+array[j+1];
                    array[j+1]=array[j]-array[j+1];
                    array[j]=array[j]-array[j+1];
                    //System.out.println(array[i]+"");
                }
            }
        }
       // 输出排序后的数组
        System.out.println("冒泡排序后");
        for(int i=0;i<array.length;i++){
            System.out.print(+array[i]+" ");
        }
    }
}
